package com.cskaoyan.service.system;


import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AdminManagementServiceImpl implements AdminManagementService{

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    @Transactional
    public CommonData<MarketAdmin> selectCommonData(Integer page, Integer limit, String sort, String order,String username) {
        PageHelper.startPage(page, limit);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectAdmin(username);
        CommonData<MarketAdmin> marketAdminCommonData = new CommonData<>();
        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(marketAdmins);
        marketAdminCommonData.setLimit(limit);
        marketAdminCommonData.setPage(page);
        marketAdminCommonData.setTotal((int) pageInfo.getTotal());
        marketAdminCommonData.setPages(pageInfo.getPages());
        marketAdminCommonData.setList(pageInfo.getList());
        return marketAdminCommonData;
    }

    @Override
    @Transactional
    public MarketAdmin insertAdmin(MarketAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        marketAdminMapper.insertAdmin(admin);
        return admin;
    }

    @Override
    @Transactional
    public MarketAdmin updateAdmin(MarketAdmin admin) {
        marketAdminMapper.updateByPrimaryKey(admin);
        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(admin.getId());
        return marketAdmin;
    }

    @Override
    public int deleteAdmin(MarketAdmin admin) {
        marketAdminMapper.deleteByPrimaryKey(admin.getId());
        return 0;
    }
}
